package com.tobeface.wechat.request.api;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.tobeface.modules.lang.Function;
import com.tobeface.modules.lang.Langs;
import com.tobeface.wechat.request.AbstractWeChatApiRequestBuilder;
import com.tobeface.wechat.request.WeChatRequest;

import java.util.List;
import java.util.Map;

/**
 * @author loudyn
 */
public class CreateMenuApiRequestBuilder extends AbstractWeChatApiRequestBuilder {

    private List<MenuBuilder> builders = Lists.newArrayList();

    /**
     *
     */
    public CreateMenuApiRequestBuilder() {
        super("https://api.weixin.qq.com/cgi-bin/menu/create", WeChatRequest.Verb.POST);
    }

    /**
     * @return
     */
    public MenuBuilder menu() {
        MenuBuilder builder = new MenuBuilder(this);
        builders.add(builder);
        return builder;
    }

    @Override
    protected void configRequest(WeChatRequest request) {
        List<Map<String, Object>> menuInfos = Langs.map(builders, new Function<MenuBuilder, Map<String, Object>>() {
            @Override
            public Map<String, Object> apply(MenuBuilder menuBuilder) {
                return menuBuilder.getMenuInfos();
            }
        });
        request.entity(ImmutableMap.of("button", menuInfos));
    }

    /**
     *
     */
    public class MenuBuilder {

        List<Map<String, Object>> buttons = Lists.newArrayList();
        Map<String, Object> props = Maps.newHashMap();
        Map<String, Object> curbutton;

        final CreateMenuApiRequestBuilder parent;

        MenuBuilder(CreateMenuApiRequestBuilder parent) {
            this.parent = parent;
        }

        Map<String, Object> getMenuInfos() {
            if (buttons.isEmpty()) {
                return props;
            }

            String name = (String) props.get("name");
            return ImmutableMap.of("name", name, "sub_button", buttons);
        }

        public MenuBuilder name(String name) {
            getActivedProps().put("name", name);
            return this;
        }

        private Map<String, Object> getActivedProps() {
            return null != curbutton ? curbutton : props;
        }

        public MenuBuilder clickType() {
            getActivedProps().put("type", "click");
            return this;
        }

        public MenuBuilder viewType() {
            getActivedProps().put("type", "view");
            return this;
        }

        public MenuBuilder key(String key) {
            getActivedProps().put("key", key);
            return this;
        }

        public MenuBuilder url(String url) {
            getActivedProps().put("url", url);
            return this;
        }

        public MenuBuilder addButton() {
            curbutton = Maps.newHashMap();
            buttons.add(curbutton);
            return this;
        }

        public MenuBuilder menu() {
            return parent.menu();
        }

        /**
         * @return
         */
        public WeChatRequest build() {
            return parent.build();
        }
    }
}
